/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.gr.jcash.util.converter;

import br.com.gr.jcash.constantes.Constantes;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author gmnascimento
 */
@FacesConverter(value = "enumConverter", forClass = Enum.class)
public class EnumConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String descricao) throws ConverterException {
        try {
            return Constantes.getEnum(descricao);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object object) throws ConverterException {
        try {
            Enum type = (Enum) object;
            return type.toString();
        } catch (Exception e) {
            return null;
        }
    }
}
